function isBipartite() : boolean;

var
	st, i, tto, v, h, t, tt : longint;
	q : array[0..1010] of longint;

begin
	result := true;
	fillchar(p, sizeof(p), -1);
	for st := 1 to n do
		if p[st] = -1 then begin
			h := 1;
			t := 1;
			q[1] := st;
			p[st] := 0;
			while h <= t do begin
				v := q[h];
				tt := length(g[v]) - 1;
				for i := 0 to tt do begin
					tto := g[v][i];
					if p[tto] = -1 then begin
						if p[v] = 0 then p[tto] := 1 else p[tto] := 0;
						inc(t);
						q[t] := tto;
					end else result := result and (p[tto] <> p[v]);
				end;
				inc(h);
			end;
		end;
end;
